module Main exposing (..)

import Browser
import Model as Ml
import View as Vi
import Msg as Ev

-- MAIN
main =
  Browser.sandbox
      { init = init
      , update = update
      , view = Vi.view
      }

-- EVENTS
init : Ml.Model
init =
    { name="STR"
    , val=10
    , character =
          { name="Skadi the Brave"
          , abilities =
                { str = 10
                , dex = 10
                , con = 10
                , int = 10
                , wis = 10
                , cha = 10
                }
          }
    }

changeVal : Ml.Model -> Int -> Ml.Model
changeVal model x =
    { name=model.name
    , val=model.val + x
    , character=model.character
    }

changeAbi : Ml.Abilities -> Ml.Ability -> Int -> Ml.Abilities
changeAbi abilities abi x =
    case abi of
        Ml.Strength ->
            { str = abilities.str + x
            , dex = 10
            , con = 10
            , int = 10
            , wis = 10
            , cha = 10
            }
        _ -> abilities

changeAbilities : Ml.Model -> Ml.Abilities -> Ml.Model
changeAbilities model abilities =
    { name=model.name
    , val=model.val
    , character=
          { name = model.character.name
          , abilities = abilities
          }
    }

-- UPDATE
update : Ev.Msg -> Ml.Model -> Ml.Model
update msg model =
  case msg of
    Ev.Increment ->
      changeVal model 1
    Ev.Decrement ->
      changeVal model -1
    Ev.IncrementStr ->
      changeAbilities model (changeAbi model.character.abilities Ml.Strength 1)
    Ev.DecrementStr ->
      changeAbilities model (changeAbi model.character.abilities Ml.Strength -1)
